﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Xml;

namespace VerticalMenu.Utility
{
	public class XmlHelper
	{
		public XmlHelper()
		{
			this.m_xmlPath = string.Empty;
			this.m_xmlDoc = null;
			this.m_xmlRootNode = null;
			this.m_xmlCurNode = null;
		}

		public XmlHelper(string xmlPath, string rootNode)
		{
			this.LoadXml(xmlPath, rootNode);
		}

		public XmlDocument XmlDoc
		{
			get
			{
				return this.m_xmlDoc;
			}
		}

		public XmlElement XmlRootNode
		{
			get
			{
				return this.m_xmlRootNode;
			}
		}

		public XmlElement XmlCurNode
		{
			get
			{
				return this.m_xmlCurNode;
			}
		}

		public void LoadXml(string xmlPath, string rootNode)
		{
			this.m_xmlPath = xmlPath;
			this.m_xmlDoc = new XmlDocument();
			try
			{
				bool flag = !File.Exists(xmlPath) || rootNode.Length == 0;
				if (flag)
				{
					XmlNode newChild = this.m_xmlDoc.CreateXmlDeclaration("1.0", "UTF-8", null);
					this.m_xmlDoc.AppendChild(newChild);
					this.m_xmlRootNode = this.m_xmlDoc.CreateElement("", rootNode, "");
					this.m_xmlDoc.AppendChild(this.m_xmlRootNode);
					this.m_xmlCurNode = this.m_xmlRootNode;
				}
				else
				{
					this.m_xmlDoc.Load(xmlPath);
					this.m_xmlRootNode = this.m_xmlDoc.DocumentElement;
					this.m_xmlCurNode = this.m_xmlRootNode;
				}
			}
			catch (Exception)
			{
				throw;
			}
		}

		public void SaveXml(string xmlPath = "")
		{
			bool flag = string.IsNullOrEmpty(xmlPath);
			if (flag)
			{
				xmlPath = this.m_xmlPath;
			}
			this.m_xmlDoc.Save(xmlPath);
		}

		public bool IsReady()
		{
			return this.m_xmlDoc != null && this.m_xmlRootNode != null && this.m_xmlCurNode != null;
		}

		public bool SelectNode(string path)
		{
			this.NodePath.Clear();
			string[] array = path.Trim().Split(XmlHelper.m_sp, StringSplitOptions.RemoveEmptyEntries);
			for (int i = 0; i < array.Length; i++)
			{
				this.NodePath.Add(array[i]);
			}
			bool flag = this.NodePath.Count == 0 || this.m_xmlRootNode.Name != this.NodePath[0];
			if (flag)
			{
				this.NodePath.Insert(0, this.m_xmlRootNode.Name);
			}
			bool result;
			try
			{
				result = this.ReSelectNode();
			}
			catch (Exception)
			{
				throw;
			}
			return result;
		}

		public List<XmlElement> GetElements()
		{
			List<XmlElement> list = new List<XmlElement>();
			bool flag = !this.IsReady();
			List<XmlElement> result;
			if (flag)
			{
				result = list;
			}
			else
			{
				foreach (object obj in this.m_xmlCurNode.ChildNodes)
				{
					XmlNode xmlNode = (XmlNode)obj;
					list.Add(xmlNode as XmlElement);
				}
				result = list;
			}
			return result;
		}

		public List<XmlElement> GetElements(string path)
		{
			List<XmlElement> result = new List<XmlElement>();
			try
			{
				this.SelectNode(path);
				result = this.GetElements();
			}
			catch (Exception)
			{
				throw;
			}
			return result;
		}

		public List<XmlAttribute> GetAttributes()
		{
			List<XmlAttribute> list = new List<XmlAttribute>();
			bool flag = !this.IsReady();
			List<XmlAttribute> result;
			if (flag)
			{
				result = list;
			}
			else
			{
				foreach (object obj in this.m_xmlCurNode.Attributes)
				{
					XmlAttribute item = (XmlAttribute)obj;
					list.Add(item);
				}
				result = list;
			}
			return result;
		}

		public List<XmlAttribute> GetAttributes(string path)
		{
			List<XmlAttribute> result = new List<XmlAttribute>();
			try
			{
				this.SelectNode(path);
				result = this.GetAttributes();
			}
			catch (Exception)
			{
				throw;
			}
			return result;
		}

		public string GetValue(string name)
		{
			string result;
			try
			{
				bool flag = !this.IsReady();
				if (flag)
				{
					result = string.Empty;
				}
				else
				{
					XmlNode xmlNode = this.m_xmlCurNode.SelectSingleNode(name);
					result = xmlNode.InnerText;
				}
			}
			catch (Exception)
			{
				result = string.Empty;
			}
			return result;
		}

		public string GetValue(string parentPath, string name)
		{
			string result;
			try
			{
				this.SelectNode(parentPath);
				result = this.GetValue(name);
			}
			catch (Exception)
			{
				result = string.Empty;
			}
			return result;
		}

		public string GetValueEx(string name, string defValue = "")
		{
			string value = this.GetValue(name);
			return string.IsNullOrEmpty(value) ? defValue : value;
		}

		public string GetValueEx(string parentPath, string name, string defValue = "")
		{
			string value = this.GetValue(parentPath, name);
			return string.IsNullOrEmpty(value) ? defValue : value;
		}

		public string GetAttribute(string name)
		{
			bool flag = !this.IsReady();
			string result;
			if (flag)
			{
				result = string.Empty;
			}
			else
			{
				result = this.m_xmlCurNode.GetAttribute(name);
			}
			return result;
		}

		public string GetAttribute(string parentPath, string name)
		{
			string result;
			try
			{
				this.SelectNode(parentPath);
				result = this.GetAttribute(name);
			}
			catch (Exception)
			{
				result = string.Empty;
			}
			return result;
		}

		public string GetAttributeEx(string name, string defValue = "")
		{
			string attribute = this.GetAttribute(name);
			return string.IsNullOrEmpty(attribute) ? defValue : attribute;
		}

		public string GetAttributeEx(string parentPath, string name, string defValue = "")
		{
			string attribute = this.GetAttribute(parentPath, name);
			return string.IsNullOrEmpty(attribute) ? defValue : attribute;
		}

		public XmlElement AddElement(string nodeName)
		{
			XmlElement result = null;
			try
			{
				bool flag = !this.IsReady();
				if (flag)
				{
					this.CreatePathNodes();
				}
				bool flag2 = !this.ReSelectNode();
				if (flag2)
				{
					return null;
				}
				XmlElement xmlElement = this.m_xmlDoc.CreateElement(nodeName);
				this.m_xmlCurNode.AppendChild(xmlElement);
				result = xmlElement;
			}
			catch (Exception)
			{
				throw;
			}
			return result;
		}

		public XmlElement AddElement(string parentPath, string nodeName)
		{
			XmlElement result = null;
			try
			{
				this.SelectNode(parentPath);
				result = this.AddElement(nodeName);
			}
			catch (Exception)
			{
				throw;
			}
			return result;
		}

		public bool SetValue(string name, string value)
		{
			bool result;
			try
			{
				bool flag = !this.IsReady();
				XmlElement xmlElement;
				if (flag)
				{
					xmlElement = this.AddElement(name);
				}
				else
				{
					xmlElement = (this.m_xmlCurNode.SelectSingleNode(name) as XmlElement);
					bool flag2 = xmlElement == null;
					if (flag2)
					{
						xmlElement = this.AddElement(name);
					}
				}
				bool flag3 = xmlElement != null;
				if (flag3)
				{
					xmlElement.InnerText = value;
					result = true;
				}
				else
				{
					result = false;
				}
			}
			catch (Exception)
			{
				result = false;
			}
			return result;
		}

		public bool SetValue(string parentPath, string name, string value)
		{
			bool result;
			try
			{
				this.SelectNode(parentPath);
				result = this.SetValue(name, value);
			}
			catch (Exception)
			{
				result = false;
			}
			return result;
		}

		public bool SetAttribute(string name, string value)
		{
			bool result;
			try
			{
				bool flag = !this.IsReady();
				if (flag)
				{
					this.CreatePathNodes();
				}
				bool flag2 = !this.ReSelectNode();
				if (flag2)
				{
					result = false;
				}
				else
				{
					this.m_xmlCurNode.SetAttribute(name, value);
					result = true;
				}
			}
			catch (Exception)
			{
				result = false;
			}
			return result;
		}

		public bool SetAttribute(string parentPath, string name, string value)
		{
			bool result;
			try
			{
				this.SelectNode(parentPath);
				result = this.SetAttribute(name, value);
			}
			catch (Exception)
			{
				result = false;
			}
			return result;
		}

		public static List<XmlElement> GetElements(XmlElement parentNode)
		{
			List<XmlElement> list = new List<XmlElement>();
			bool flag = parentNode == null;
			List<XmlElement> result;
			if (flag)
			{
				result = list;
			}
			else
			{
				foreach (object obj in parentNode.ChildNodes)
				{
					XmlNode xmlNode = (XmlNode)obj;
					list.Add(xmlNode as XmlElement);
				}
				result = list;
			}
			return result;
		}

		public static List<XmlAttribute> GetAttributes(XmlElement node)
		{
			List<XmlAttribute> list = new List<XmlAttribute>();
			bool flag = node == null;
			List<XmlAttribute> result;
			if (flag)
			{
				result = list;
			}
			else
			{
				foreach (object obj in node.Attributes)
				{
					XmlAttribute item = (XmlAttribute)obj;
					list.Add(item);
				}
				result = list;
			}
			return result;
		}

		public static string GetValue(XmlElement node)
		{
			bool flag = node == null;
			string result;
			if (flag)
			{
				result = string.Empty;
			}
			else
			{
				result = node.InnerText;
			}
			return result;
		}

		public static string GetValueEx(XmlElement node, string defValue = "")
		{
			string value = XmlHelper.GetValue(node);
			return string.IsNullOrEmpty(value) ? defValue : value;
		}

		public static string GetAttribute(XmlElement node, string name)
		{
			bool flag = node == null;
			string result;
			if (flag)
			{
				result = string.Empty;
			}
			else
			{
				result = node.GetAttribute(name);
			}
			return result;
		}

		public static string GetAttributeEx(XmlElement node, string name, string defValue = "")
		{
			string attribute = XmlHelper.GetAttribute(node, name);
			return string.IsNullOrEmpty(attribute) ? defValue : attribute;
		}

		public static XmlElement AddElement(XmlElement parentNode, string nodeName)
		{
			bool flag = parentNode == null;
			XmlElement result;
			if (flag)
			{
				result = null;
			}
			else
			{
				XmlDocument ownerDocument = parentNode.OwnerDocument;
				bool flag2 = ownerDocument == null;
				if (flag2)
				{
					result = null;
				}
				else
				{
					XmlElement xmlElement = ownerDocument.CreateElement(nodeName);
					parentNode.AppendChild(xmlElement);
					result = xmlElement;
				}
			}
			return result;
		}

		public static bool SetValue(XmlElement node, string value)
		{
			bool flag = node != null;
			bool result;
			if (flag)
			{
				node.InnerText = value;
				result = true;
			}
			else
			{
				result = false;
			}
			return result;
		}

		public static bool SetAttribute(XmlElement node, string name, string value)
		{
			bool flag = node != null;
			bool result;
			if (flag)
			{
				node.SetAttribute(name, value);
				result = true;
			}
			else
			{
				result = false;
			}
			return result;
		}

		private bool ReSelectNode()
		{
			bool result;
			try
			{
				bool flag = this.m_xmlDoc == null || this.m_xmlRootNode == null;
				if (flag)
				{
					result = false;
				}
				else
				{
					string text = string.Empty;
					foreach (string str in this.NodePath)
					{
						text += "/";
						text += str;
					}
					this.m_xmlCurNode = (this.m_xmlRootNode.SelectSingleNode(text) as XmlElement);
					result = (this.m_xmlCurNode != null);
				}
			}
			catch (Exception)
			{
				throw;
			}
			return result;
		}

		private void CreatePathNodes()
		{
			try
			{
				bool flag = this.m_xmlDoc == null || this.m_xmlRootNode == null;
				if (!flag)
				{
					XmlNode xmlNode = this.m_xmlRootNode;
					for (int i = 1; i < this.NodePath.Count; i++)
					{
						string text = this.NodePath[i];
						XmlNode xmlNode2 = xmlNode.SelectSingleNode(text);
						bool flag2 = xmlNode2 == null;
						if (flag2)
						{
							xmlNode2 = this.m_xmlDoc.CreateElement(text);
							xmlNode.AppendChild(xmlNode2);
						}
						xmlNode = xmlNode2;
					}
				}
			}
			catch (Exception)
			{
				throw;
			}
		}

		private static char[] m_sp = new char[]
		{
			'\\',
			'/'
		};

		private string m_xmlPath;

		private XmlDocument m_xmlDoc;

		private XmlElement m_xmlRootNode;

		private XmlElement m_xmlCurNode;

		private List<string> NodePath = new List<string>();
	}
}
